class LH {
    options = {
        responseType : 'json'
    }
    //对类进行初始化
    constructor(method = 'GET',url,data=null,options) {
        //给类添加属性并且进行赋值操作
        this.method = method;
        this.url = url;
        this.data = this.formatData(data)
        Object.assign(this.options,options)
    }
    //格式化表单数据
    formatData(data){
        if(typeof data != 'object' || data == null) data = {};
        let form = new FormData();
        for(const [name,value] of Object.entries(data)){
            form.append(name,value);
        }
        return form;
    }
    //声明get 方法
    static get(url,options){
        return new this('GET',url,null,options).xhr();
    }
    //声明post方法
    static post(url,data,options){
        return new this('POST',url,data,options).xhr();
    }
    //核心处理网络请求
    xhr(){
        return new Promise((resolve,reject) => {
            const xhr = new XMLHttpRequest();
            xhr.open(this.method,this.url);
            xhr.responseType = this.options.responseType;
            xhr.send(this.data);
            xhr.onload = function () {
                if(xhr.status != 200){
                    //调用接口失败的时候的调用
                    reject({status:xhr.status,error:xhr.statusText});
                }else{
                    //成功之后的回调
                    resolve(xhr.response);
                }
            }
            xhr.onerror = function (error) {
                reject(error)
            }
        });
    }
}